using System;

namespace SqlQueryBuilder.Clauses
{
    /// <summary>
    /// Abstract class for where clauses that allow to format the diferent type of values to SQL type
    /// </summary>
    public class AbstractValueClause
    {
        /// <summary>
        /// Formats the SQL value.
        /// </summary>
        /// <param name="unformatedValue">The unformated value.</param>
        /// <returns></returns>
        public virtual string FormatSQLValue(object unformatedValue)
        {
            if (unformatedValue == null) return "NULL";

            switch (unformatedValue.GetType().Name)
            {
                case "String":
                    return string.Format("'{0}'",
                        ((string)unformatedValue).Replace("'", "''"));

                case "Boolean":
                    return (bool)unformatedValue ? "1" : "0";

                case "DBNull":
                    return "NULL";

                case "SqlLiteral":
                    return ((SqlLiteral)unformatedValue).Value;

                case "SqlDateTime": 
                    return ((SqlDateTime) unformatedValue).getDate();

                case "DateTime":
                    return new SqlDateTime((DateTime) unformatedValue).getDate();

                default:
                    return unformatedValue.ToString();
            }
        }
    }
}